package cn.zsxmlv.modules.sys.service.impl;

import cn.zsxmlv.modules.sys.dao.SysRoleMenuDao;
import cn.zsxmlv.modules.sys.entity.SysRoleMenuEntity;
import cn.zsxmlv.modules.sys.service.SysRoleMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zs
 * @description:
 * @date: 2019/3/5 14:16
 */
@Service
@Transactional
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {

    @Override
    public void saveOrUpdate(Integer roleId, List<Integer> menuIdList) {
        // 删除角色与菜单的关系
        deleteBatch(new Integer[]{roleId});

        if (menuIdList.size() == 0) {
            return;
        }

        ArrayList<SysRoleMenuEntity> list = new ArrayList<>(menuIdList.size());
        for (Integer menuId : menuIdList) {
            SysRoleMenuEntity build = SysRoleMenuEntity.builder()
                    .menuId(menuId)
                    .roleId(roleId)
                    .build();
            list.add(build);
        }

        this.insertBatch(list);
    }

    @Override
    public int deleteBatch(Integer[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }

    @Override
    public List<Integer> queryMenuIdList(Integer roleId) {
        return baseMapper.queryMenuIdList(roleId);
    }
}
